package com.applet.applet_shangcheng.service;

import com.applet.applet_shangcheng.dao.ImagesMapper;
import com.applet.applet_shangcheng.dao.ShufflingFigureMapper;
import com.applet.applet_shangcheng.entity.Announcement;
import com.applet.applet_shangcheng.entity.ShufflingFigure;
import com.applet.applet_shangcheng.utlis.DateUtils;
import com.applet.applet_shangcheng.utlis.PageResult;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class ShufflingFigureService {

    @Autowired
    private ShufflingFigureMapper shufflingFigureMapper;

    @Autowired
    private ImagesMapper imagesMapper;

    /*public List<ShufflingFigure> findAll(){
        List<ShufflingFigure> list = shufflingFigureMapper.findAll();
        return list;
    };*/
    public List<ShufflingFigure> findGoods(){
        List<ShufflingFigure> list = shufflingFigureMapper.findAll();
        return list;
    };

    public int insertGoods(ShufflingFigure shufflingFigure){
        int result = shufflingFigureMapper.insertGoods(shufflingFigure);
        return result;
    };

    public int deleteGoods(int goodsId){
        int result = shufflingFigureMapper.deleteGoods(goodsId);
        return result;
    };

    public int updateStatus(int goodsId,int status){
        int result = shufflingFigureMapper.updateStatus(goodsId, status);
        return result;
    };

    @Transactional(rollbackFor = Exception.class)
    public int updateGoods(ShufflingFigure shufflingFigure){
        ShufflingFigure shufflingFigureDB = shufflingFigureMapper.findById(shufflingFigure.getGoodsId());
        imagesMapper.updateImageStatus(shufflingFigureDB.getImage());
        int result = shufflingFigureMapper.updateGoods(shufflingFigure);
        return result;
    }

    public ShufflingFigure findByGoodsId(int goodsId) {
        return shufflingFigureMapper.findById(goodsId);
    }

    /*public PageResult findAll(String queryString, Integer currentPage, Integer pageSize) {
        PageHelper.startPage(currentPage,pageSize );
        Page<ShufflingFigure> page = shufflingFigureMapper.findAll1(queryString);
        //PageInfo<Announcement> pageInfo = new PageInfo<>(announcementList);
        return new PageResult(page.getTotal(),page.getResult() );
    }*/

    public PageResult findAll(String queryString, Integer currentPage, Integer pageSize) {
        PageHelper.startPage(currentPage,pageSize );
        List<ShufflingFigure> page = shufflingFigureMapper.findAll1(queryString);
        for (ShufflingFigure shufflingFigure: page) {
            Date createDate = shufflingFigure.getCreateDate();
            try {
                String date2String = DateUtils.parseDate2String(createDate);
                shufflingFigure.setCreateDate1(date2String);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        PageInfo<ShufflingFigure> pageInfo = new PageInfo<>(page);
        return new PageResult(pageInfo.getTotal(),pageInfo.getList() );
    }

    public ShufflingFigure findById(int id) {
        return shufflingFigureMapper.findById(id);

    }
}
